package com.lly.songhigh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.application.BaseApplication;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.SingerDetail;
import com.lly.songhigh.fragment.AlbumFragment;
import com.lly.songhigh.fragment.MVFragment;
import com.lly.songhigh.fragment.SingerInformationFragment;
import com.lly.songhigh.fragment.SingleFragment;
import com.lly.songhigh.volley.StringRequestUtf;
import com.lly.songhigh.volley.VolleySingleton;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 遇博 on 15/10/27.
 */
public class SingerDetailActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private ImageView singer_picture_IV, detail_toolbar_back;
    private String url, albumUrl, MVUrl, inforUrl;
    private String singerName;
    private RadioGroup chinese_sin_tab;
    private FragmentTransaction transaction;
    private CollapsingToolbarLayout detail_collapsing_toolbar;
    private int singerId;
    private TextView singerDetailSingerName;
    private VolleySingleton volleySingleton;
    private TextView detail_listen_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singer_detail_activity);
        BaseApplication.getInstance().addActivity(this);
        initView();
        initData();
    }

    private void initData() {
        volleySingleton = VolleySingleton.getInstance();
        Intent intent = getIntent();
        singerId = intent.getIntExtra("singerId", 0);
        final StringRequestUtf stringRequestUtf = new StringRequestUtf(URL.SINGER_PIC_START + singerId + URL.SINGER_PIC_END, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SingerDetail detail = gson.fromJson(response, SingerDetail.class);
                Picasso.with(SingerDetailActivity.this).load(detail.getData().getPicUrl())
                        .placeholder(R.mipmap.placeholder_detail_radio_ban)
                        .error(R.mipmap.placeholder_detail_radio_ban)
                        .into(singer_picture_IV);
                singerName = detail.getData().getName();
                detail_collapsing_toolbar.setTitle(singerName);
                singerDetailSingerName.setText(singerName);
                detail_collapsing_toolbar.setCollapsedTitleTextColor(StringResouce.WHITE_COLOR);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
        initUrl();
        transaction = this.getSupportFragmentManager().beginTransaction();
        SingleFragment singleFragment = new SingleFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        singleFragment.setArguments(bundle);
        transaction.replace(R.id.tabView1, singleFragment);
        transaction.commit();
    }

    private void initView() {
        singer_picture_IV = (ImageView) findViewById(R.id.singer_picture_IV);
        chinese_sin_tab = (RadioGroup) findViewById(R.id.chinese_sin_tab);
        singerDetailSingerName = (TextView) findViewById(R.id.singer_detail_singerName);
        detail_toolbar_back = (ImageView) findViewById(R.id.detail_toolbar_back);
        detail_listen_count = (TextView) findViewById(R.id.detail_listen_count);
        detail_listen_count.setVisibility(View.GONE);
        detail_collapsing_toolbar = (CollapsingToolbarLayout) findViewById(R.id.detail_collapsing_toolbar);
        detail_collapsing_toolbar.setExpandedTitleTextAppearance(R.style.CollapsingToolbarLayoutText);
        // 设置监听
        chinese_sin_tab.setOnCheckedChangeListener(this);
        detail_toolbar_back.setOnClickListener(this);
    }

    private void initUrl() {
        url = URL.SINGER_DETAIL_START + singerId + URL.SINGER_DETAIL_SINGLE_END;
        albumUrl = URL.SINGER_DETAIL_START + singerId + URL.SINGER_DETAIL_ALBUM_END;
        MVUrl = URL.SINGER_DETAIL_START + singerId + URL.SINGER_DETAIL_MV_END;
        inforUrl = URL.SINGER_DETAIL_START + singerId + URL.SINGER_DETAIL_INFO_END;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.male_singer:
                transaction = this.getSupportFragmentManager().beginTransaction();
                SingleFragment singleFragment = new SingleFragment();
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                singleFragment.setArguments(bundle);
                transaction.replace(R.id.tabView1, singleFragment);
                transaction.commit();
                break;
            case R.id.female_singer:
                transaction = this.getSupportFragmentManager().beginTransaction();
                AlbumFragment albumFragment = new AlbumFragment();
                Bundle albumBundle = new Bundle();
                albumBundle.putString("albumUrl", albumUrl);
                albumFragment.setArguments(albumBundle);
                transaction.replace(R.id.tabView1, albumFragment);
                transaction.commit();
                break;
            case R.id.combination_singer:
                transaction = this.getSupportFragmentManager().beginTransaction();
                MVFragment mvFragment = new MVFragment();
                Bundle MVBundle = new Bundle();
                MVBundle.putString("MVUrl", MVUrl);
                mvFragment.setArguments(MVBundle);
                transaction.replace(R.id.tabView1, mvFragment);
                transaction.commit();
                break;
            case R.id.information_singer:
                transaction = this.getSupportFragmentManager().beginTransaction();
                SingerInformationFragment singerInformationFragment = new SingerInformationFragment();
                Bundle inforBundle = new Bundle();
                inforBundle.putString("inforUrl", inforUrl);
                singerInformationFragment.setArguments(inforBundle);
                transaction.replace(R.id.tabView1, singerInformationFragment);
                transaction.commit();
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_toolbar_back:
                finish();
                break;
        }
    }
}